#include "EventLoopThreadPool.h"
#include "EventLoopThread.h"
#include "EventLoop.h"

EventLoopThreadPool::EventLoopThreadPool(EventLoop *mainLoop, size_t threadNum)
: _mainLoop(mainLoop)
, _IOthreadNum(threadNum)
, _next(0)
, _started(false)
{
}
EventLoopThreadPool::~EventLoopThreadPool()
{
}

void EventLoopThreadPool::start()
{
    for (size_t i = 0; i < _IOthreadNum; ++i)
    {
        auto thread = std::make_unique<EventLoopThread>();
        EventLoop *loop = thread->startLoop(); // 启动线程并获取EventLoop
        _loops.push_back(loop);
        _subLoop.push_back(std::move(thread));
    }
}
EventLoop *EventLoopThreadPool::getNextLoop()
{
    if(_loops.empty())
    {
        return _mainLoop; // 如果没有subReactor，返回主Reactor
    }

    EventLoop *loop = _loops[_next];
    _next = (_next + 1) % _loops.size(); // 循环
    return loop; // 返回subReactor
}